package com.aplink.generic.xmlparser.ctrlformat;

import org.w3c.dom.Node;

import android.text.TextUtils;

import com.aplink.generic.constant.type.EditTextType;
import com.aplink.generic.pojo.controlspec.EditTextSpec;
import com.aplink.generic.xmlparser.XmlPaserUtil;

public class EditTextFormat {
	private static final String DIGITS = "digits";
	private static final String DISPLAY_FORMAT = "displayFormat";
	private static final String DISPLAY_TYPE = "displayType";
	private static final String INPUTTYPE = "inputtype";
	private static final String ISREADONLY = "readonly";
	private static final String MAXLENGTH = "maxlength";
	private static final String MINLENGTH = "minlength";
	private static final String SINGLE_LINE = "isSingleLine";

	public static EditTextSpec parse(final Node node) {
		final EditTextSpec editTextSpec = new EditTextSpec();
		if (node.hasAttributes()) {
			String value = XmlPaserUtil.getAttrValue(node,
			        EditTextFormat.ISREADONLY);
			if (!TextUtils.isEmpty(value)) {
				if (value.equalsIgnoreCase("Y")) {
					editTextSpec.setReadOnly(true);
				}
			}

			value = XmlPaserUtil.getAttrValue(node, MINLENGTH);
			if (!TextUtils.isEmpty(value)) {
				editTextSpec.setMinLength(Integer.parseInt(value));
			} else {
				editTextSpec.setMinLength(EditTextSpec.MIN_LENGTH);
			}

			value = XmlPaserUtil.getAttrValue(node, MAXLENGTH);
			if (!TextUtils.isEmpty(value)) {
				editTextSpec.setMaxLength(Integer.parseInt(value));
			} else {
				editTextSpec.setMaxLength(EditTextSpec.MAX_LENGTH);
			}

			value = XmlPaserUtil.getAttrValue(node, INPUTTYPE);
			if (!TextUtils.isEmpty(value)) {
				editTextSpec.setDataInputType(EditTextType.get(value));
			}

			value = XmlPaserUtil.getAttrValue(node, DISPLAY_FORMAT);
			if (!TextUtils.isEmpty(value)) {
				editTextSpec.setDisplayFormat(value);
			}
			value = XmlPaserUtil.getAttrValue(node, DIGITS);
			if (!TextUtils.isEmpty(value)) {
				editTextSpec.setDigist(value);
			}

			value = XmlPaserUtil.getAttrValue(node, DISPLAY_TYPE);
			if (!TextUtils.isEmpty(value)) {
				editTextSpec.setDisplayType(value);
			}

			value = XmlPaserUtil.getAttrValue(node, SINGLE_LINE);
			if (!TextUtils.isEmpty(value)) {
				editTextSpec.setSingleLine(value.equalsIgnoreCase("Y"));
			}
		}
		return editTextSpec;
	}
}
